package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ExamPapersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ExamPapersForm;
import com.youlai.boot.system.model.query.ExamPapersQuery;
import com.youlai.boot.system.model.vo.ExamPapersVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 试卷题目前端控制层
 *
 * @author youlaitech
 * @since 2025-07-03 10:57
 */
@Tag(name = "试卷题目接口")
@RestController
@RequestMapping("/api/v1/exam-papers")
@RequiredArgsConstructor
public class ExamPapersController  {

    private final ExamPapersService examPapersService;

    @Operation(summary = "试卷题目分页列表")
    @GetMapping("/page")
   // @PreAuthorize("@ss.hasPerm('system:exam-papers:query')")
    public PageResult<ExamPapersVO> getExamPapersPage(ExamPapersQuery queryParams ) {
        IPage<ExamPapersVO> result = examPapersService.getExamPapersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增试卷题目")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:exam-papers:add')")
    public Result<Void> saveExamPapers(@RequestBody @Valid ExamPapersForm formData ) {
        boolean result = examPapersService.saveExamPapers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取试卷题目表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:exam-papers:edit')")
    public Result<ExamPapersForm> getExamPapersForm(
        @Parameter(description = "试卷题目ID") @PathVariable Long id
    ) {
        ExamPapersForm formData = examPapersService.getExamPapersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改试卷题目")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:exam-papers:edit')")
    public Result<Void> updateExamPapers(
            @Parameter(description = "试卷题目ID") @PathVariable Long id,
            @RequestBody @Validated ExamPapersForm formData
    ) {
        boolean result = examPapersService.updateExamPapers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除试卷题目")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:exam-papers:delete')")
    public Result<Void> deleteExamPaperss(
        @Parameter(description = "试卷题目ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = examPapersService.deleteExamPaperss(ids);
        return Result.judge(result);
    }
}
